<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class auth_model extends CI_Model
{
    public function auth_model()
    {
        parent::__construct();
    }
    //---------- Login function
    /**
     * check valid user 
     */
    public function checkUSR($username, $pass)
    {
        $query = $this->db->get_where('users', array('username' => $username, 'pass' => $pass));
        if ($query->num_rows() > 0)
            return true;
        return false;
    }
    /**
     *  check user locked
     */
    public function isLocked($username)
    {
        $query = $this->db->get_where('users', array('username' => $username, 'locked' => 'Lock'));
        if ($query->num_rows() > 0)
            return true;
        return false;
    }

    /**
     *  register session login
     */
    public function addSession($username)
    {
        $query = $this->db->get_where('users', array('username' => $username));
        $this->session->set_userdata(array(
            "USERNAME" => $query->row("username"),
            "LASTNAME" => $query->row("lastname"),
            "USID" => $query->row("id"),
            "GID" => $query->row("gid")));
        $_SESSION["CI_LOGIN"] = "ok";
    }

    public function userInfo()
    {
        return array(
            'username'  => $this->session->userdata('USERNAME'),
            'lastname'  => $this->session->userdata('LASTNAME'),
            'uid'       => $this->session->userdata('USID'),
            'gid'       => $this->session->userdata('GID'),
        );
    }
    /**
     * return true if logged and false if not logged
     */
    public function isLogin()
    {
        if (!isset($_SESSION["CI_LOGIN"]) || $_SESSION["CI_LOGIN"] != 'ok')
            return false;
        return true;
    }

    public function userid()
    {
        return $this->session->userdata('USID');
    }

    public function username()
    {
        return $this->session->userdata('USERNAME');
    }

    public function gid()
    {
        return $this->session->userdata('GID');
    }

    /**
     * user or member list 
     */
    public function userList($target)
    {
        $limit = 25;
        $query = $this->db->get_where("users", null, $limit, $target);
        $paging = $this->home_model->paging_Admin("users", $query->num_rows(), $limit);
        return array("query" => $query, "paging" => $paging);
    }
    public function GetUserById($id)
    {
        $query = $this->db->get_where('users', array('id' => $id));
        return array('query' => $query);
    }
    // checking
    public function isUser($id)
    {
        $query = $this->db->get_where('users', array('id' => $id));
        if ($query->num_rows() > 0)
            return true;
        return false;
    }

    public function checkForm()
    {
        $this->form_validation->set_rules("firtname", 'fistname', "trim|required");
        $this->form_validation->set_rules("lastname", 'lastname', "trim|required");
        return $this->form_validation->run();
    }
    public function checkFormAdd()
    {
        $this->form_validation->set_rules("username", 'username', "trim|required");
        $this->form_validation->set_rules("firtname", 'fistname', "trim|required");
        $this->form_validation->set_rules("lastname", 'lastname', "trim|required");
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('acti', '', 'trim');
        return $this->form_validation->run();
    }
    public function checkFormPass()
    {
        $this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        return $this->form_validation->run();
    }
    public function doUpdate()
    {
        $this->load->helper('text');
        $field['firtname'] = $this->input->post('firtname');
        $field['lastname'] = $this->input->post('lastname');
        $field['acti'] = $this->input->post('acti');

        if (isset($_POST['idx'])) {
            $id = decrypt($_POST["idx"]);
            return $this->db->update('users', $field, array('id' => $id));
        }
    }
    public function doAdd()
    {
        $this->load->helper('text');
        $field['username'] = $this->input->post('username');
        $field['firtname'] = $this->input->post('firtname');
        $field['lastname'] = $this->input->post('lastname');
        $field['pass'] = imd5(trim($this->input->post('password')));
        $field['acti'] = $this->input->post('acti');
        $field['create'] = time();
        return $this->db->insert('users', $field);
    }
    public function doChangePass()
    {
        $field['pass'] = imd5(trim($this->input->post('password')));
        return $this->db->update('users', $field, array('id' => $this->session->userdata('USID')));
    }

    public function _doUpdate()
    {
        $this->load->helper('text');
        $field['username'] = $this->input->post('username');
        $field['firtname'] = $this->input->post('firtname');
        $field['lastname'] = $this->input->post('lastname');
        $field['pass'] = imd5(trim($this->input->post('password')));
        $field['acti'] = $this->input->post('acti');
        $field['create'] = time();
        return $this->db->insert('users', $field);
    }
}
